<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
      <el-button @click="to_send(row)" v-if="in_right_admin('MessageTemplate/send')" size="small">发送消息</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {vue_template} from "@/js/dom_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if(in_right_admin('MessageTemplate/add')) btns['添加'] = function(){ iframe('message_template/edit') }
    if(in_right_admin('MessageTemplate/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let type_map = _config('status_alias_map.message_template.type');
    let forms_config = {
      title: {label:'关键词',name:'where[title|content][like]',type:'text_one',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', order:true},
      title: {name:'title', label:'标题', width: '200px'},
      template_code:{name:'template_code', label:'模板code', min_width:'200px'},
      type:{name:'type', label:'消息类型', width:'85px',type:'select_one',config:type_map},
      gateway:{name:'gateway', label:'短信网关', width:'85px',type:'select_one',config:[]},
      content:{name:'content', label:'消息内容', type:'text_more', min_width:'200px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'140px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/message_template/'+(is_del?'recycle':'list'),
        title: "消息列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          s94Mjs.each(list, function (row){
              let data = JSON.parse(row.param);
              for (const k in data) {
                  data[k] += '('+k+')';
              }
              row.content_info = vue_template(row.content, data);
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    in_right_admin: in_right_admin,
    to_edit(row){
      let can_edit = in_right_admin('MessageTemplate/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('message_template/edit?'+s94Mjs.param_encode(param))
    },
    to_send(row){
      iframe('message_template/send?id='+row.id)
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/message_template/config_map',cache:true},function(res){
      app.config.list_cols.gateway.config = res.data['gateway'];
      return false
    })
  }
}
</script>
